<div class="index">
<?php
if($isAjax==0)
{ 
?>
<?php
	 echo $this->Html->css('jquery.multiselect');
	 echo $this->Html->script('jquery.multiselect');
?>
<script><!--
	$(function() {
		$( ".datepick" ).datepicker({
			 changeMonth: true,
			dateFormat: "dd-mm-yy",
			showOn: "both",
			buttonImage:$('#root').val()+"img/calendar.jpg",
			buttonImageOnly: true});
		
		$(".datepick").keydown(function(e) {
			if ( e.keyCode != 13) {}
			else {e.preventDefault(); }   
			});

		$( "#ngaybd" ).datepicker({
				showOtherMonths: true,
				selectOtherMonths: true,
				changeMonth: true,								
				dateFormat: "dd-mm-yy",
				showOn: "both",
				buttonImage:$('#root').val()+"img/calendar.jpg",
				buttonImageOnly: true
			});
			$( "#ngaykt" ).datepicker({
				showOtherMonths: true,
				selectOtherMonths: true,
				changeMonth: true,								
				dateFormat: "dd-mm-yy",
				showOn: "both",
				buttonImage:$('#root').val()+"img/calendar.jpg",
				buttonImageOnly: true
			});
			$("#ngaybd").change(function(){
				date = $.datepicker.parseDate('dd-mm-yy',$("#ngaybd").val());
				var nam = date.getFullYear();
				var thang = date.getMonth();
				var ngay = date.getDaysInMonth();
				var newdate = new Date(nam,thang,ngay);
				newdate = $.datepicker.formatDate('dd-mm-yy', newdate);
				$("#ngaykt").datepicker( "option", "minDate",date);
				$("#ngaykt").datepicker( "option", "maxDate",newdate);
			});

	});
</script>

<?php echo $form->create('Kqbanhang');?>
<?php echo $form->input('root',array('id'=>'root','type'=>'hidden','value'=>$this->webroot));?>
<?php echo '<h2>Kết quả bán hàng lưu động</h2>'.$form->input('nhanvien_id', array('id'=>'nhanvien_id','label'=>'Nhân viên','options'=>$nhanviens,'selected'=>$nhanvien_id,'empty'=>'  --  Chọn Nhân Viên  --  '));?>

    <div id='kqbanhangngay'>
		<table>
		  <tr>
		    <td><?php echo $form->input('ngaybd',array('id'=>'ngaybd','type'=>'text','label'=>'Từ','value'=>date('d-m-Y')));?></td>
		    <td><?php echo $form->input('ngaykt',array('id'=>'ngaykt','type'=>'text','label'=>'Đến','value'=>date('d-m-Y')));?></td>
		  	<th><?php echo $js->submit('  Xem  ', array(
										'url'=> array('controller'=>'kqbanhangs','action'=>'kqbhld',$nhanvien_id),
		 								'update' => '#kqbhld'));?></th>
		  </tr>
		</table>

		<div id='kqbhld'>
		<?php

			 if(!empty($hanghoabhlds) && !empty($chitieungaybhlds)) {?>
             <?php echo $this->Html->link(__('Xuất Excel', true), array('action' => 
        'kqbhldxls',$donvi_id,$nhanvien_id,date('d-m-Y'),date('d-m-Y')));
             $tongdk = $hanghoabhlds;
			$tongkq = $hanghoabhlds;
              ?>
			<table id ='tables'>
				<tr>
				<th rowspan=3>STT</th>
				<th rowspan=3>Trung Tâm</th>
				<th rowspan=3>Nhân Viên</th>
				<th rowspan=3>Đội</th>
				<th rowspan=3>Địa Điểm</th>
				<th rowspan=3>Ngày</th>
				<th rowspan=3>Thành viên tham gia</th>
				<th colspan=<?php echo count($hanghoabhlds)*3?>>Chỉ tiêu</th>
				<th rowspan=3>Ghi chú</th>
				
				</tr>
				<tr>
				<?php foreach ($hanghoabhlds as $keyhh =>$valhh):?>
				<th colspan=3><?php echo $valhh; $tongdk[$keyhh]=0; $tongkq[$keyhh]=0; ?></th>
				<?php endforeach;?>
				</tr>
				<tr>
				<?php foreach ($hanghoabhlds as $keyhh =>$valhh):?>
				<th>Chỉ Tiêu</th>
				<th>Kết quả</th>
				<th>% Hoàn Thành </th>
				<?php endforeach;?>
				</tr>
				<?php
					$i=0; 
					 foreach ($chitieungaybhlds as $chitieungay):
					  $i++;
				?>
				<tr>
				<td><?php echo $i?></td>
				<td><?php echo $chitieungay['Nhanvien']['Donvi']['madv']?></td>
				<td><?php echo $chitieungay['Nhanvien']['tennv']?></td>
				<td><?php echo $chitieungay['Kehoachbhld']['tendoi']?></td>
				<td><?php echo $chitieungay['Kehoachbhld']['diadiem']?></td>
				<td><?php echo date('d-m-Y',strtotime($chitieungay['Kehoachbhld']['thoigian']))?></td>
				<td><?php echo $chitieungay['Kehoachbhld']['nhansu']?></td>
				<?php foreach ($hanghoabhlds as $keyhh =>$valhh):?>
				<td>
				<?php
					$cosl = 0;
					$cokq = 0;
					$pt = 0;
					foreach ($chitieungay['Chitieungay'] as $ctngay)
					{
						if($ctngay['hanghoa_id']==$keyhh)
						{
				 			echo $ctngay['soluong'];
				 			$tongdk[$keyhh] +=$ctngay['soluong'];
				 			$cosl = 1;
				 			break;
						}	
					}
					if($cosl==0)
						echo 0;
				?>
				</td>
				<td>
				<?php
					foreach ($chitieungay['Chitieungay'] as $ctngay)
					{
						if($ctngay['hanghoa_id']==$keyhh)
						{
				 			echo $ctngay['ketqua'];
				 			$tongkq[$keyhh] +=$ctngay['ketqua'];
				 			$cokq=1;
				 			break;
						}	
					}
					if($cokq==0)
						echo 0;
				?>
				</td>
				<td>
				<?php
					foreach ($chitieungay['Chitieungay'] as $ctngay)
					{
						if($ctngay['hanghoa_id']==$keyhh)
						{
							$num=(empty($ctngay['soluong'])? 0 :$ctngay['ketqua']*100/$ctngay['soluong']);
				 			echo number_format(round($num,2),0,',','.').'%';
				 			$pt = 1;
				 			break;
						}	
					}
					if($pt==0)
						echo "0%";
				?>
				</td>
				<?php endforeach;?>
				<td><?php echo $chitieungay['Kehoachbhld']['ghichu']?></td>
				</tr>
				<?php 
                endforeach;?> 
                <tr ><td colspan=7 style="text-align:center;"> <b>Tổng</b> </td>
                <?php
					foreach ($hanghoabhlds as $keyh=>$valh)
					{?>
				 		<td><?php echo $tongdk[$keyh];?></td>	
				 		<td><?php echo $tongkq[$keyh];?></td>
				 		<td><?php echo number_format((round($tongdk[$keyh]==0?0:$tongkq[$keyh]*100/$tongdk[$keyh],2)),0,',','.').'%';?></td>
					<?php }?>
                </tr>
				</table>
					<p>
					<?php
					echo $this->Paginator->counter(array(
					'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true)
					));
					?>	</p>
				
					<div class="paging">
						<?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled'));?>
					 | 	<?php echo $this->Paginator->numbers();?>
				 |
						<?php echo $this->Paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled'));?>
					</div>
					<?php }else{echo "<h2>Không có dữ liệu</h2>";} ?>
		</div>
		
	</div>
<?php
}
else
{?>
			<?php

			 if(!empty($hanghoabhlds) && !empty($chitieungaybhlds)) {?>
             <?php echo $this->Html->link(__('Xuất Excel', true), array('action' => 
        'kqbhldxls',$donvi_id,$nhanvien_id,$this->Form->value('Kqbanhang.ngaybd'),$this->Form->value('Kqbanhang.ngaykt'))); ?>
				<?php
				  $tongdk = $hanghoabhlds;
					$tongkq = $hanghoabhlds;
				?>
				<table id ='tables'>
				<tr>
				<th rowspan=3>STT</th>
				<th rowspan=3>Trung Tâm</th>
				<th rowspan=3>Nhân Viên</th>
				<th rowspan=3>Đội</th>
				<th rowspan=3>Địa Điểm</th>
				<th rowspan=3>Ngày</th>
				<th rowspan=3>Thành viên tham gia</th>
				<th colspan=<?php echo count($hanghoabhlds)*3?>>Chỉ tiêu</th>
				<th rowspan=3>Ghi chú</th>
				
				</tr>
				<tr>
				<?php foreach ($hanghoabhlds as $keyhh =>$valhh):?>
				<th colspan=3><?php echo $valhh; $tongdk[$keyhh]=0; $tongkq[$keyhh]=0; ?></th>
				<?php endforeach;?>
				</tr>
				<tr>
				<?php foreach ($hanghoabhlds as $keyhh =>$valhh):?>
				<th>Chỉ Tiêu</th>
				<th>Kết quả</th>
				<th>% Hoàn Thành </th>
				<?php endforeach;?>
				</tr>
				<?php
					$i=0; 
					 foreach ($chitieungaybhlds as $chitieungay):
					  $i++;
				?>
				<tr>
				<td><?php echo $i?></td>
				<td><?php echo $chitieungay['Nhanvien']['Donvi']['madv']?></td>
				<td><?php echo $chitieungay['Nhanvien']['tennv']?></td>
				<td><?php echo $chitieungay['Kehoachbhld']['tendoi']?></td>
				<td><?php echo $chitieungay['Kehoachbhld']['diadiem']?></td>
				<td><?php echo date('d-m-Y',strtotime($chitieungay['Kehoachbhld']['thoigian']))?></td>
				<td><?php echo $chitieungay['Kehoachbhld']['nhansu']?></td>
				<?php foreach ($hanghoabhlds as $keyhh =>$valhh):?>
				<td>
				<?php
					$cosl = 0;
					$cokq = 0;
					$pt = 0;
					foreach ($chitieungay['Chitieungay'] as $ctngay)
					{
						if($ctngay['hanghoa_id']==$keyhh)
						{
				 			echo $ctngay['soluong'];
				 			$tongdk[$keyhh] +=$ctngay['soluong'];
				 			$cosl = 1;
				 			break;
						}	
					}
					if($cosl==0)
						echo 0;
				?>
				</td>
				<td>
				<?php
					foreach ($chitieungay['Chitieungay'] as $ctngay)
					{
						if($ctngay['hanghoa_id']==$keyhh)
						{
				 			echo $ctngay['ketqua'];
				 			$tongkq[$keyhh] +=$ctngay['ketqua'];
				 			$cokq=1;
				 			break;
						}	
					}
					if($cokq==0)
						echo 0;
				?>
				</td>
				<td>
				<?php
					foreach ($chitieungay['Chitieungay'] as $ctngay)
					{
						if($ctngay['hanghoa_id']==$keyhh)
						{
							$num=(empty($ctngay['soluong'])? 0 :$ctngay['ketqua']*100/$ctngay['soluong']);
				 			echo number_format(round($num,2),0,',','.').'%';
				 			$pt = 1;
				 			break;
						}	
					}
					if($pt==0)
						echo "0%";
				?>
				</td>
				<?php endforeach;?>
				<td><?php echo $chitieungay['Kehoachbhld']['ghichu']?></td>
				</tr>
				<?php 
                endforeach;?> 
                <tr ><td colspan=7 style="text-align:center;"> <b>Tổng</b> </td>
                <?php
					foreach ($hanghoabhlds as $keyh=>$valh)
					{?>
				 		<td><?php echo $tongdk[$keyh];?></td>	
				 		<td><?php echo $tongkq[$keyh];?></td>
				 		<td><?php echo number_format((round($tongdk[$keyh]==0?0:$tongkq[$keyh]*100/$tongdk[$keyh],2)),0,',','.').'%';?></td>
					<?php }?>
                </tr>
				</table>
					<p>
					<?php
					echo $this->Paginator->counter(array(
					'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true)
					));
					?>	</p>
				
					<div class="paging">
						<?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled'));?>
					 | 	<?php echo $this->Paginator->numbers();?>
				 |
						<?php echo $this->Paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled'));?>
					</div>
					<?php }else{echo "<h2>Không có dữ liệu</h2>";} ?>
<?php } ?>
</div>